import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '08 Bar-TabBar-选项卡',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      body: DemoPage(),
    );
  }
}


class DemoPage extends StatefulWidget{
  @override
  DemoPageState createState() => DemoPageState();
}

class DemoPageState extends State<DemoPage>{
  ScrollController _scrollController;
  TabController _tabController;


  @override

  void initState(){
    super.initState();
    _scrollController = new ScrollController();
    //length 和数量一致
    _tabController = new TabController(length: 7, vsync: this);
  }

  void dispose(){
    _scrollController.dispose();
    _tabController.dispose();
    super.dispose();
  }

  Widget build(BuildContext context) {
    return SizedBox(
      height: 500,
      child: Scaffold(
        appBar: AppBar(
          title: Text('AppBar'),
          //前置图标
          leading: Icon(Icons.home),
          //应用栏颜色
          backgroundColor: Colors.green,
          //选项卡
          bottom: TabBar(controller: _tabController,
            //是否可以滚动
            isScrollable: true,
            tabs: [
              Tab(text: '1',icon: Icon(Icons.add),),
              Tab(text: '2',),
              Tab(text: '3',),
              Tab(text: '4',),
              Tab(text: '5',),
              Tab(text: '6',),
              Tab(text: '7',),
            ],
          ),
        ),
        //选项卡视图
        body: TabBarView(controller: _tabController,
          children: [
            Text('data1'),
            Text('data2'),
            Text('data3'),
            Text('data4'),
            Text('data5'),
            Text('data6'),
            Text('data7'),
          ],
        ),
      ),
    );
  }
}